﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Com.AMap.Api.Maps;
using Com.AMap.Api.Maps.Model;
using System.Windows.Media;
using jbgswin.Resources;

namespace jbgswin
{
    public partial class addpeople : PhoneApplicationPage
    {
        AMap amap;
        AMapGeolocator mylocation;
        AMapMarker marker;
        public static LatLng s;
        //圆
       
        private static string qid;
        AMapCircle circle;

        public addpeople()
        {

            InitializeComponent();
            BuildLocalizedApplicationBar();
            this.ContentPanel.Children.Add(amap = new AMap());
            amap.Loaded += amap_Loaded;
            amap.Tap += amap_Tap;
            this.rd1.IsChecked = true;
            
        }

        void amap_Loaded(object sender, RoutedEventArgs e)
        {
            //throw new NotImplementedException();]
            amap.MoveCamera(CameraUpdateFactory.NewLatLngZoom(new LatLng(29.868038, 121.550331), 15));
        }
        void amap_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (this.rd2.IsChecked == true)
            {

                //throw new NotImplementedException();
                Point p = e.GetPosition(amap);//获得在map容器的像素坐标

                s = amap.GetProjection().FromScreenLocation(p);
                marker = amap.AddMarker(new AMapMarkerOptions()
                {
                    Position = s,
                    Title = "Title",
                    Snippet = "Snippet",
                    IconUri = new Uri("Images/AZURE.png", UriKind.Relative),
                });
            }
        }
         private void Button_Click(object sender, RoutedEventArgs e)
        {

             if (qid!=null)
             {
                 dituwz d = App.myditu.Single(h => h.nowid.ToString() == qid);
                 d.ly = s.latitude.ToString();
                 d.lx = s.longitude.ToString();
                 if (!App.toupdates.Exists(h => h.nowid.ToString() == qid))
                 {
                     App.toupdates.Add(d);

                 }
                 else
                 {
                     dituwz ds = App.toupdates.Single(h => h.nowid.ToString() == qid);
                     ds.ly = s.latitude.ToString();
                     ds.lx = s.longitude.ToString();

                 }
                 if (MessageBox.Show("更新成功请查看记录", "更新成功", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                 {
                     NavigationService.Navigate(new Uri("/peoplelist.xaml", UriKind.Relative));
                 }
             } 
             else
             {
             
            App.myditu.Add(new dituwz()
            {
                //riqi = System.DateTime.Now,
                nowid = App.myditu.Count + 1,
                ly = s.latitude.ToString(),
                lx = s.longitude.ToString(),
                name = "aagc",
                shuoming = "aasm",
            });
            App.toupdates.Add(new dituwz()
            {
                //riqi = System.DateTime.Now,
                nowid = App.myditu.Count + 1,
                ly = s.latitude.ToString(),
                lx = s.longitude.ToString(),
                name = "aagc",
                shuoming = "aasm",
            });
            if (MessageBox.Show("是否结束添加，并查看记录", "添加成功", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                NavigationService.Navigate(new Uri("/peoplelist.xaml", UriKind.Relative));
            }
            else
            {
                return;
               
             
        
            }
           
             }
        }
        private void BuildLocalizedApplicationBar()
        {
            // 将页面的 ApplicationBar 设置为 ApplicationBar 的新实例。
            ApplicationBar = new ApplicationBar();

            // 创建新按钮并将文本值设置为 AppResources 中的本地化字符串。
            ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.download.rest.png", UriKind.Relative));
            // appBarButton.Text = AppResources.AppBarButtonText;
            appBarButton.Text = "定位";
            ApplicationBar.Buttons.Add(appBarButton);
            appBarButton.Click += appBarButton_Click;
            ApplicationBarIconButton appBarButton2 = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.delete.rest.png", UriKind.Relative));
            // appBarButton.Text = AppResources.AppBarButtonText;
            appBarButton2.Text = "清除标记点";
            ApplicationBar.Buttons.Add(appBarButton2);
            // appBarButton.Click += appBarButton_Click;
            appBarButton2.Click += appBarButton2_Click;
            // 使用 AppResources 中的本地化字符串创建新菜单项。
            ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
            ApplicationBar.MenuItems.Add(appBarMenuItem);
        }

        void appBarButton2_Click(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            amap.Clear();
        }

      

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {

        }

        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {

        }
       
        void appBarButton_Click(object sender, EventArgs e)
        {
            mylocation = new AMapGeolocator();
            mylocation.Start();
            //触发位置改变事件
            mylocation.PositionChanged += mylocation_PositionChanged;
        }
        void mylocation_PositionChanged(AMapGeolocator sender, AMapPositionChangedEventArgs args)
        {
            s = args.LngLat;

            this.Dispatcher.BeginInvoke(() =>
            {
                //GeoSearch(args.LngLat);
/*
                if (marker == null)
                {*/
                    //添加圆
                    circle = amap.AddCircle(new AMapCircleOptions()
                    {
                        Center = args.LngLat,//圆点位置
                        Radius = (float)args.Accuracy,//半径
                        FillColor = Color.FromArgb(80, 100, 150, 255),
                        StrokeWidth = 2,//边框粗细
                        StrokeColor = Color.FromArgb(80, 0, 0, 255),//边框颜色

                    });

                    //添加点标注，用于标注地图上的点
                    marker = amap.AddMarker(
                   new AMapMarkerOptions()
                   {
                       Position = args.LngLat,//图标的位置
                       Title = "我的位置",
                       Snippet = args.LngLat.ToString(),
                       IconUri = new Uri("Images/marker_gps_no_sharing.png", UriKind.Relative),//图标的URL
                       Anchor = new Point(0.5, 0.5),//图标中心点

                   });
               /* }
                else
                {
                    //点标注和圆的位置在当前经纬度
                    marker.Position = args.LngLat;
                    circle.Center = args.LngLat;
                    circle.Radius = (float)args.Accuracy;//圆半径
                }
*/
                //设置当前地图的经纬度和缩放级别
                amap.MoveCamera(CameraUpdateFactory.NewLatLngZoom(args.LngLat, 15));
               /* Debug.WriteLine("定位精度：" + args.Accuracy + "米");
                Debug.WriteLine("定位经纬度：" + args.LngLat);*/
            });
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (NavigationContext.QueryString.Count==1)
            {
                qid = NavigationContext.QueryString["id"].ToString();
            }
          //  int a = 0;
        }

    }
}
       
 